package me.keiwu.image.util;


import me.keiwu.image.util.bean.EditDataBean;
import org.im4java.core.ConvertCmd;
import org.im4java.core.IM4JavaException;
import org.im4java.core.IMOperation;

import java.io.IOException;
import java.util.List;

/**
 * Created by kei on 27/10/2017.
 */
public class ImageUtil {


    public static void main(String[] args) {
        String bgSrc = "/Users/kei/Desktop/test.png";
        String outSrc = "/Users/kei/Desktop/test2.png";
        String fontSrc = "/Library/Fonts/siyuan.ttf";
        String fontColor = "#000000";
        Integer fontSize = 40;

        IMOperation op = new IMOperation();
        op.addImage(bgSrc);
        ImageUtil.operationAddText(op,
                EditDataBean.createTextBean("search啊啊啊啊", 100, 85, fontSrc, fontColor, fontSize));

        // 输出
        op.addImage(outSrc);
        ConvertCmd cmd = new ConvertCmd();
        try {
            cmd.run(op);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (IM4JavaException e) {
            e.printStackTrace();
        }
    }



    public static void operationAddText(IMOperation operation, EditDataBean text)  {
        StringBuilder sb = new StringBuilder();
        if (text.getRotateDegree() != null) {
            sb.append("rotate ").append(text.getRotateDegree());
        }
        sb.append(" text ").append(text.getX()).append(",").append(text.getY()).append(" ")
                .append("'").append(text.getText()).append("'");
        if (text.getFontFamily() != null) {
            operation.font(text.getFontFamily());
        }
        operation.pointsize(text.getFontSize()).fill(text.getFontColor()).draw(sb.toString());
    }


    public static void operationAddTextList(IMOperation operation, List<EditDataBean> textList) {
        StringBuilder sb = new StringBuilder();
        textList.forEach(text -> {
            sb.setLength(0);
            if (text.getRotateDegree() != null) {
                sb.append("rotate ").append(text.getRotateDegree());
            }
            sb.append(" text ")
                    .append(text.getX()).append(",").append(text.getY()).append(" ")
                    .append(text.getText());
            if (text.getFontFamily() != null) {
                operation.font(text.getFontFamily());
            }
            operation.pointsize(text.getFontSize())
                    .fill(text.getFontColor())
                    .draw(sb.toString());
        });
    }

}
